package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.Utils.SMSUtils;
import com.itheima.reggie.Utils.ValidateCodeUtils;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 发送手机短信验证码
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/sendMsg")
    public R<String> sedMsg(@RequestBody User user, HttpSession session) {
        //1:获取手机号
        String phone = user.getPhone();
        //2：这里因为阿里云的短信服务我没申请到，所有直接用验证码代替了，提供了相应的工具类
        //2.1：先判断一下传进来的phone是否为空
        if (StringUtils.isNotEmpty(phone)) {
            //生成随机的四位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            //到时候我们直接来控制台看验证码
            log.info("code={}", code);
            //下面是模拟的调用阿里云提供的短信服务api来完成短信发送，提供了相应的api发送类
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //我们将收到的验证码存入到session中
            session.setAttribute(phone, code);
            return R.success("手机验证码发送成功");
        }
        return R.error("你填写的手机号为空");
    }

    /**
     * 移动端用户登录
     *
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> sedMsg(@RequestBody Map map, HttpSession session) {
        log.info(map.toString());
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        String code = map.get("code").toString();
        //从session中获取保存的验证码
        Object codeinsession = session.getAttribute(phone);
        //进行验证码的比对（页面提交的验证码和session中保存的验证码对比）
        if (codeinsession != null && codeinsession.equals(code)) {
            //判断手机号是否为新号，如果是，注册一下
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone, phone);
            User user = userService.getOne(queryWrapper);
            if (user == null) {
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            return R.success(user);

        }

        return R.error("登录失败");
    }
    /**
     * 用户
     * @param request
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest request){
        //清楚Session中保存的当前登录员工的id
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

}
